import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { CheckItemDetail} from "../check-item-detail/check-item-detail";
import { ScanPage} from "../../scan/scan";
import { ElevatorCheck} from "../elevator-check/elevator-check";
import { InformDetail } from "../../../pages/refresher/inform-detail/inform-detail";
import { NoticeDetail } from "../../../pages/refresher/notice-detail/notice-detail";
import { SignPage } from "../../sign/sign";
import { ApplyRepairDetailPage } from "../../repair-apply-detail/apply-repair-detail";
import { SafeControlDetailPage } from "../../supervision/safe-control-detail/safe-control-detail";
import { appConfig, constant } from "../../../providers/config";
import { RecordCheckPage } from "../record-check/record-check";
import { RecordMedia } from "../record-media/record-media";
import * as moment from "moment";
import {SafeControlPage} from "../safe-control/safe-control";
@Component({
  templateUrl: "check-item.html"
})
export class CheckItem {
  flag: boolean = true;
  producePartyStatus:string = "未检查";
  userPartyStatus: string = "未检查";
  elevatorStatus: string = "未检查";
  checkPartyStatus: string = "未检查";
  definedStatus: string = "未检查";
  checkedNum: number ;
  type:string ;// 检查项目类型
  itemList: any = [] //检查的项目结果
  elevatorList: any = []
  id: string
  private title: string; //标题
  private list: any = [];
  private pageSize: number = 10;
  private currentPage: number = 1;
  private currentUserId: any;
  private partyId: any;
  private parentId: any;
  isNotice: boolean = false;
  isRead: any = "0";
  refresher: any;
  userName: any;
  totalResult: any = [];
  result: any = [];
  private PLAN_DICT_TYPE: any = [];
  CHECK_STATE: any = [];
  producePartyResult:any = [];
  userPartyResult:any = [];
  elevatorResult: any = [];
  checkPartyResult: any = [];
  defineResult: any = [];
  state: string  = null;
  constructor(
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private httpService: HttpService,
    private storage: Storage,
    private navController: NavController,
    public alertCtrl: AlertController
  ) {
      //
      this.state = this.navParams.get("state");

      //存储检查结果
      this.type = this.navParams.get("type");
      this.itemList = this.navParams.get("checkRes");

      if (this.type == "1") {
        this.storage.set("producePartyResult",this.itemList);
      }
      if (this.type == "2") {
          this.storage.set("userPartyResult",this.itemList);
      }
      /*if (this.type == "3") {
          this.storage.set("elevatorResult",this.itemList);
      }*/
      if (this.type == "4") {
          this.storage.set("defineResult",this.itemList);
      }
      if (this.type == "5") {
          this.storage.set("checkPartyResult",this.itemList);
      }

      //设置检查状态
      this.storage.get("producePartyStatus").then(data=>{
          if (data != null && data != "") {
              this.producePartyStatus = data;
          }else {
              this.producePartyStatus = "未检查";
          }
      });
      this.storage.get("userPartyStatus").then(data=>{
          if (data != null && data != "") {
              this.userPartyStatus = data;
          }else {
              this.userPartyStatus = "未检查";
          }
      });
      this.storage.get("checkPartyStatus").then(data=>{
          if (data != null && data != "") {
              this.checkPartyStatus = data;
          }else {
              this.checkPartyStatus = "未检查";
          }
      });
      this.storage.get("definedStatus").then(data=>{
          if (data != null && data != "") {
              this.definedStatus = data;
          }else {
              this.definedStatus = "未检查";
          }
      });

  }
    ionViewWillEnter(){
        this.storage.get("elevatorResult").then(data =>{
            let array = [];
            if (data !=null && data.length > 0) {
                for (let i = 0; i < data.length ; i++) {
                    if (array.indexOf(data[i].deviceNum) == -1) {
                        array.push(data[i].deviceNum);
                    }
                }
            }
            this.elevatorStatus = "已检查 " + array.length +" 台";
        });
    }
  //
    check(type) {
        if (type == "1") {
            this.storage.get("producePartyResult").then(data=>{
                this.push(CheckItemDetail,type,data,this.checkedNum);
            });
        }
        if (type == "2") {
            this.storage.get("userPartyResult").then(data=>{
                this.push(CheckItemDetail,type,data,this.checkedNum);
            });
        }
        if (type == "3") {
            this.push(ElevatorCheck,type,null,null);
        }
        if (type == "4") {
            this.storage.get("defineResult").then(data=>{
                this.push(CheckItemDetail,type,data,this.checkedNum);
            });
        }
        if (type == "5") {
            this.storage.get("checkPartyResult").then(data=>{
                this.push(CheckItemDetail,type,data,this.checkedNum);
            });
        }
    }
    //

    push(page, type, data,checkedNum) {
        this.navController.push(page,{
            id:this.navParams.get("id"),
            pdId: this.navParams.get("pdId"),
            beCkedPartyName: this.navParams.get("beCkedPartyName"),
            beCkedPartyId: this.navParams.get("beCkedPartyId"),
            pdType: this.navParams.get("pdType"),
            type:type,
            state: this.state,
            checkResult:data,
            workType:"check",
            checkedNum:checkedNum,
        });
    }

    submit() {
        this.storage.get("producePartyResult").then(data => {
            this.totalResult = [];
            this.totalResult.push(data);
        });
        this.storage.get("userPartyResult").then(data => {
            this.totalResult.push(data);
        });
        this.storage.get("elevatorResult").then(data => {
            this.totalResult.push(data);
        });
        this.storage.get("checkPartyResult").then(data => {
            this.totalResult.push(data);
        });
        this.storage.get("defineResult").then(data => {
            this.totalResult.push(data);
            this.result = [];
            for (let i = 0; i < this.totalResult.length; i++) {
                if (this.totalResult[i] != null && this.totalResult[i].length > 0) {
                    for (let j = 0; j < this.totalResult[i].length; j++) {
                        this.result.push(this.totalResult[i][j]);
                    }
                }
            }
            this.navController.push(SafeControlPage, {
                totalResult: this.result,
                beCkedPartyName: this.navParams.get("beCkedPartyName"),
                beCkedPartyId: this.navParams.get("beCkedPartyId"),
                type: this.navParams.get("type"),
                id: this.navParams.get("id"),
                pdId: this.navParams.get("pdId"),
                pdType: this.navParams.get("pdType"),
                state: this.state,
            });
        });
    }
}
